
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;
import com.huizhan.web.view.*;

/**
 * 用户项目角色api
 */
 
@Api(tags = "AccountProjectRole",description = "用户项目角色")
@RestController
public class AccountProjectRoleController {
    
    @Autowired
    private AccountProjectRoleDao dao;
   
    /**
     * 获取用户项目角色集合
     * @return api结果
     */
    @ApiOperation(value = "获取用户项目角色集合")
    @RequestMapping(value =  "/api/AccountProjectRole/",method = {RequestMethod.GET})
    public ApiResult<List<AccountProjectRole>> Get(){
         ApiResult<List<AccountProjectRole>> result=new ApiResult<List<AccountProjectRole>>();
        try 
        {
            List<AccountProjectRole> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取用户项目角色信息
     * @param id 用户项目角色ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取用户项目角色信息")
    @RequestMapping(value =  "/api/AccountProjectRole/{id}",method = {RequestMethod.GET})
    public ApiResult<AccountProjectRole> Get(@PathVariable Long id){
        ApiResult<AccountProjectRole> result=new ApiResult<AccountProjectRole>();
        try
        {
            AccountProjectRole model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改用户项目角色信息
     * @param id 用户项目角色ID
     * @param model 用户项目角色信息
     * @return api结果
     */
    @ApiOperation(value = "修改用户项目角色信息")
    @RequestMapping(value =  "/api/AccountProjectRole/{id}",method = {RequestMethod.PUT})
    public ApiResult<AccountProjectRole> Edit(@PathVariable Long id,@RequestBody AccountProjectRole model){
        ApiResult<AccountProjectRole> result=new ApiResult<AccountProjectRole>();
        try
        {
            AccountProjectRole role=dao.findByAccountIdAndProjectId(model.getAccountId(),model.getProjectId());
            
            if(role!=null&&!role.getAccountProjectRoleId().equals(model.getAccountProjectRoleId())){
                result.setHasError(true);
                result.setMessage("该用户只能绑定当前项目的一个角色");
            }else{
             model.setAccountProjectRoleId(id);
            dao.save(model);
            result.setData(model);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加用户项目角色信息
     * @param model 用户项目角色信息
     * @return api结果
     */
    @ApiOperation(value = "添加用户项目角色信息")
    @RequestMapping(value =  "/api/AccountProjectRole/",method = {RequestMethod.POST})
    public ApiResult<AccountProjectRole> Add(@RequestBody AccountProjectRole model){
         ApiResult<AccountProjectRole> result=new ApiResult<AccountProjectRole>();
        try
        {
            if(dao.findByAccountIdAndProjectId(model.getAccountId(),model.getProjectId())!=null){
                result.setHasError(true);
                result.setMessage("该用户只能绑定当前项目的一个角色");
            }else{
            model.setCreateDate(new Date());
            dao.save(model);
            result.setData(model);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 查询账号项目角色集合
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询账号项目角色集合")
    @RequestMapping(value =  "/api/AccountProjectRole/Search",method = {RequestMethod.POST})
    public ApiResult<List<AccountProjectRole>> Search(@RequestBody PageRole model){
         ApiResult<List<AccountProjectRole>> result=new ApiResult<List<AccountProjectRole>>();
         
        try
        {    
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"creaet_date") ;            
            Page<AccountProjectRole> page = dao.findByWhere(model.getName(),model.getProject(),model.getRole(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据用户ID获取项目集合
     * @param id 用户ID
     * @return api结果
     */
    @ApiOperation(value = "根据用户ID获取项目集合")
    @RequestMapping(value =  "/api/AccountProjectRole/Project/{accountId}",method = {RequestMethod.GET})
    public ApiResult<List<AccountProjectRole>> Account(@PathVariable String accountId){
        ApiResult<List<AccountProjectRole>> result=new ApiResult<List<AccountProjectRole>>();
        try
        {
            List<AccountProjectRole> model= dao.findByAccountId(accountId);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 删除用户项目角色信息
     * @param id 用户项目角色ID
     * @return api结果
     */
    @ApiOperation(value = "删除用户项目角色信息")
    @RequestMapping(value =  "/api/AccountProjectRole/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

